/*
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package eu.fastl.dragster.log.exec;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import eu.fastl.dragster.log.config.XlsExportConfigReader;
import eu.fastl.dragster.log.service.impl.LogParserServiceSimpleImpl;
import eu.fastl.dragster.log.service.impl.XlsExportServiceSimplImpl;

/**
 * Command line tool for converting Performance Log files into Excel
 * spreadsheets
 *
 * @author Ernst Fastl
 */
public class XlsExportExec {

    private static final Logger LOGGER = LoggerFactory.getLogger(XlsExportExec.class);

    public static void main(String[] args) {

        Options options = createOptions();

        CommandLineParser parser = new PosixParser();
        CommandLine cmdLine = null;
        try {
            cmdLine = parser.parse(options, args);
        } catch (ParseException e) {
            LOGGER.error("Exception while parsing Command Line Arguments", e);
            System.out.println("");
            printHelpAndExit(options);
        }
        if (cmdLine.hasOption("h")) {
            printHelpAndExit(options);
        }
        if (cmdLine.hasOption("v")) {
            printVersion();
            System.exit(1);
        }

        XlsExportServiceSimplImpl service = new XlsExportServiceSimplImpl();
        service.setLogParserService(new LogParserServiceSimpleImpl());
        service.setXlsExportConfigReader(new XlsExportConfigReader());

        try {
            service.exportToSpreadSheet(cmdLine.getOptionValue("i"), cmdLine.getOptionValue("o"), cmdLine.getOptionValue("c"));
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private static void printHelpAndExit(Options options) {
        printHelp(options);
        System.exit(1);
    }

    private static void printVersion() {
        System.out.println("Dragster XLS Exporter v0.1");
    }

    private static void printHelp(Options options) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(XlsExportExec.class.getName(), options);
        System.out.println("");
    }

    @SuppressWarnings("static-access")
    private static Options createOptions() {
        Options retval = new Options();

        Option outputFileNameOption = OptionBuilder.withLongOpt("output-filename")
                .withDescription("The name of the XLS file to create / overwrite").hasArg().withArgName("filename")
                .isRequired().create("o");

        Option inputFileNameOption = OptionBuilder.withLongOpt("input-filename")
                .withDescription("The name of Log file to process").hasArg().withArgName("filename").isRequired()
                .create("i");

        Option configFileNameOption = OptionBuilder.withLongOpt("config-filename")
                .withDescription("The name of an XML configuratio file to use").hasArg().withArgName("filename")
                .create("c");

        Option versionOption = OptionBuilder.withLongOpt("version")
                .withDescription("display the current version number of the application").create("v");

        Option helpOption = OptionBuilder.withLongOpt("help").withDescription("display this help screen").create("h");

        retval.addOption(configFileNameOption);
        retval.addOption(outputFileNameOption);
        retval.addOption(inputFileNameOption);
        retval.addOption(versionOption);
        retval.addOption(helpOption);

        return retval;
    }

}
